package com.wbs.controller;

import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import com.wbs.service.CartService;
import com.wbs.utils.JwtUtil;
import com.wbs.vo.CartVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 芝梦
 * @Description: 购物车控制层
 * @DateTime: 2024/2/26 19:40
 */
@RequestMapping("cart")
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private JwtUtil jwtUtil;

    //加入购物车
    @GetMapping("addCart")
    public Result addCart(@RequestParam Long skuId, @RequestParam Integer num, @RequestHeader String token){
        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        cartService.addCart(skuId,num,id);
        return Result.RESPONSE(ResultEnums.ADD_CART_SUCCESS);
    }

    //查询购物车
    @GetMapping("getCat")
    public Result getCat(@RequestHeader String token){
        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        List<CartVO> cartVOList = cartService.getCart(id);
        return Result.SUCCESS().setData(cartVOList);
    }
    //查询购物车List<CartVO>
    @GetMapping("getCatList")
    public List<CartVO> getCatList(@RequestHeader String token){
        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        List<CartVO> cartVOList = cartService.getCart(id);
        return cartVOList;
    }
    //清除购物车信息
    @DeleteMapping("clearCatList")
    public void clearCatList(@RequestParam String id){
        cartService.clearCatList(id);
    }


}
